/*----------------------------------------------------------------------------*\
| /gamemode/ragdolls.lua               | ragdoll system                  | SRV |
\*----------------------------------------------------------------------------*/

local meta = FindMetaTable( "Player" )
meta.Rag		= nil

local function RagdollKillPlayer( ply )
	if ply.Rag and ply.Rag != nil then
		ply.Rag:Remove()
		ply.Rag = nil
		ply:SetNetworkedBool( "ragdolled",  false )
		
		zday.infection.Cure( ply )
		ply:Kill()
	end
	
end

function meta:Ragdoll()
	if !self:IsRagdolled() then
		if self:InVehicle() then
			self:ExitVehicle()
		end
		local pos 	= self:GetPos()
		local model	= self:GetModel()
		local rag	= ents.Create( "prop_ragdoll" )
		rag:SetPos( pos )
		rag:SetModel( model )
		rag:Spawn()
		rag:Activate()
		rag:SetOwner( self )
		self:Spectate( OBS_MODE_IN_EYE )
		self:SpectateEntity( rag )
		self:DrawViewModel( false )
		// TODO: Drop weapons for others to loot
		self:StripWeapons()
		self:StripAmmo()
		SendChatMessage( self:GetName() .. " is dying...", CHAT_WARN )
		self.Rag 	= rag
		self:SetNetworkedBool( "ragdolled",  true )
		self:SetNetworkedEntity( "myragdoll", rag )
		timer.Create( "PlayerKillTimer" .. self:UniqueID(), ZDayConfig["RagdollDeathAfter"], 1, RagdollKillPlayer, self )
		return true
	else
		print( "ERROR: Player is already ragdolled!" )
		return false
	end
end

function meta:UnRagdoll()
	
	if self:IsRagdolled() then
	
		local pos 	= self.Rag:GetPos()
		local model	= self.Rag:GetModel()
		
		self:Spawn()
		self:UnSpectate()
		self:DrawViewModel( true )
		self:SetPos( pos + Vector( 0, 0, 10 ) ) -- Spawn them in the air abit so they don't get stuck.
		
		local timername = "PlayerKillTimer" .. self:UniqueID()
		
		if timer.IsTimer( timername ) then
		
			timer.Destroy( timername )
		
		end
		
		self:SetNetworkedBool( "ragdolled",  false )
		
		self.Rag:Remove()
		self.Rag = nil
		
		return true
	
	else
		
		print( "ERROR: Can not un-ragdoll a non ragdolled player!" )
		return false
		
	end
	
end

function meta:IsRagdolled()
	
	return self:GetNetworkedBool( "ragdolled",  false )
	
end

function RagdollWeaponPickup( ply )
	
	if ply:IsRagdolled() then
		return false
	end
	
end
hook.Add( "PlayerCanPickupWeapon", "RagdollWeaponPickup", RagdollWeaponPickup )

function DoRagdoll( ent, inflictor, attacker, amount, dmginfo )
	
	if !ent || !ent:IsValid() || !ent:IsPlayer() || ent:IsRagdolled() then return end
	
	if ent:Health() - amount <= 10 then
		
		ent:SetHealth( 1 )
		ent:Ragdoll()
		return false
		
	end
	
end
hook.Add( "EntityTakeDamage", "DoRagdoll", DoRagdoll )

function CheckDeath( ply )

    if ply:IsRagdolled() then
		RagdollKillPlayer( ply )
    end
	
end
hook.Add( "PlayerDeath", "CheckDeath", CheckDeath )

function RevivePlayerKeyHook( ply, key )

    if key == IN_USE then

        local hp = 20 // Needs to be changed to use the skills system.

		local pos = ply:GetShootPos()
		local ang = ply:GetAimVector()
		
		if !pos || !ang then return end
		
		local tracedata = {}
		tracedata.start = pos
		tracedata.endpos = pos + ( ang * 80 )
		tracedata.filter = ply
		
		local trace = util.TraceLine(tracedata)
		
		if trace.HitNonWorld then
		
			target = trace.Entity
		
			if target && target:IsValid() && target:GetClass() == "prop_ragdoll" then

				ragply = target:GetOwner()		
				
				if !ragply || !ragply:IsValid() || ragply == ply then return end

				if ragply:UnRagdoll() then
				
					ragply:SetHealth( hp )
			
					SendChatMessage( ragply:GetName() .. " has been saved by " .. ply:GetName(), CHAT_WARN )
				
				end
				
			end
			
		end
		
    end
	
end
hook.Add( "KeyPress", "RevivePlayerKeyHook", RevivePlayerKeyHook )
